package com.pingao.mp.modular.fls.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pingao.mp.config.dict.DictConfig;
import com.pingao.mp.modular.fls.entity.Activity;
import com.pingao.mp.modular.fls.entity.ActivityAwards;
import com.pingao.mp.modular.fls.entity.ActivityWinResult;
import com.pingao.mp.modular.fls.service.ActivityAwardsService;
import com.pingao.mp.modular.fls.service.ActivityService;
import com.pingao.mp.modular.fls.service.ActivityWinResultService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动 前端控制器
 * </p>
 *
 * @author 闻品高
 * @since 2020-01-15
 */
@Controller
@RequestMapping("/fls/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityAwardsService activityAwardsService;
    @Autowired
    private ActivityWinResultService activityWinResultService;

    @Autowired
    private DictConfig dictConfig;
    @GetMapping({"","/index"})
    @RequiresPermissions("fls.activity.index")
    public String index(ModelMap modelMap){
        modelMap.addAttribute("status",JSON.toJSON(dictConfig.getFlags()));
        return "fls/activity/index";
    }

    @GetMapping("/list")
    @ResponseBody
    @RequiresPermissions("sys.user.index")
    public Object list(@RequestParam("pageNumber") Integer pageNumber,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam(value = "searchText", required = false) String searchText,
                       @RequestParam("sortName") String sortName,
                       @RequestParam("sortOrder") String sortOrder,
                       @RequestParam(value = "status",required = false) Integer status
    ) {
        return activityService.getActivityList(pageNumber, pageSize, searchText, sortName, sortOrder,status);
    }

    @GetMapping("/add")
    @RequiresPermissions("fls.activity.add")
    public String add(ModelMap modelMap){
        modelMap.addAttribute("status",JSON.toJSON(dictConfig.getFlags()));
        return "fls/activity/add";
    }

    @PostMapping("/add")
    @ResponseBody
    @RequiresPermissions("fls.activity.add")
    public Object add(Activity activity){
        Map<String, Object> map = new HashMap<>();
        map.put("status",activityService.saveActivity(activity));
        return map;
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("fls.activity.edit")
    public String edit(ModelMap modelMap, @PathVariable("id") Long id) throws Exception {
        modelMap.addAttribute("status",JSON.toJSON(dictConfig.getFlags()));
        Activity activity = activityService.getById(id);
        if(activity == null){
            throw new Exception("对象不存在 或已被删除");
        }
        List<ActivityAwards> activityAwards = activityAwardsService.list(new QueryWrapper<ActivityAwards>().eq(ActivityAwards.COL_ACTIVITY_ID, id));
        activity.setAwards(JSON.toJSONString(activityAwards));
        modelMap.addAttribute("activity",activity);
        int isEdit = 1;
        //判断活动是否已经开始，或已经有人参与活动了
        if(activity.getStartingTime().isBefore(LocalDateTime.now()) || activityWinResultService.count(new QueryWrapper<ActivityWinResult>().eq(ActivityWinResult.COL_ACTIVITY_ID,id)) > 0){
            isEdit = 0;
        }
        modelMap.addAttribute("isEdit",isEdit);
        return "fls/activity/edit";
    }

    @PostMapping("/edit")
    @RequiresPermissions("fls.activity.edit")
    @ResponseBody
    public Object edit(
            Activity activity
    ) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("status",activityService.editActivity(activity));
        return map;
    }

    @PostMapping("/delete")
    @RequiresPermissions("fls.activity.delete")
    @ResponseBody
    public Object delete(
            @RequestParam(value = "ids[]") List<Long> ids
    ) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("status",activityService.deleteActivity(ids));
        map.put("msg","删除成功");
        return map;
    }
}